package com.jinzw.jform.rows

import android.content.Context
import android.graphics.Color
import android.support.v7.widget.AppCompatCheckBox
import android.view.ViewGroup
import android.widget.CheckBox
import android.widget.EditText
import android.widget.RelativeLayout

import com.jinzw.jform.core.DimensionUtils

/**
 * Created by jin on 18-2-10.
 */
class CheckRow(context: Context) : AbstractTextRow<Boolean>(context) {
    var checkbox:CheckBox?=null
    var value:Boolean =false

    override fun value(): Boolean? {
        return value;
    }

    fun customizeCheckBox(block: ((CheckRow, CheckBox) -> Unit)) {
        this.customizeCheckBox = block
    }
    protected var customizeCheckBox: ((CheckRow, CheckBox) -> Unit) = { row, editText ->
        editText.text=""
//        editText.textSize = 16f


    }

    init {
        onCreateView<CheckRow> {
            val layout = RelativeLayout(context)
            onCreateCheckView()
            val checkBoxLayoutParams = if (isEvenly) {
                RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, DimensionUtils.dpToPx(evenlyWith))
            } else {
                RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
            }
            checkBoxLayoutParams.leftMargin = DimensionUtils.dpToPx(DEFAULT_MARGIN_LEFT)
            checkBoxLayoutParams.topMargin = DimensionUtils.dpToPx(DEFAULT_MARGIN_TOP)
            checkBoxLayoutParams.bottomMargin = DimensionUtils.dpToPx(DEFAULT_MARGIN_BOTTOM)
            checkBoxLayoutParams.rightMargin = DimensionUtils.dpToPx(DEFAULT_MARGIN_RIGHT)
            checkBoxLayoutParams.addRule(RelativeLayout.CENTER_VERTICAL)
            checkBoxLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT)
            checkbox?.layoutParams=checkBoxLayoutParams
            layout.addView(checkbox)

            createTitleView()
            val titleLayoutParams = RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
            titleLayoutParams.leftMargin = DimensionUtils.dpToPx(DEFAULT_MARGIN_LEFT)
            titleLayoutParams.topMargin = DimensionUtils.dpToPx(DEFAULT_MARGIN_TOP)
            titleLayoutParams.bottomMargin = DimensionUtils.dpToPx(DEFAULT_MARGIN_BOTTOM)
            titleLayoutParams.rightMargin = DimensionUtils.dpToPx(DEFAULT_MARGIN_RIGHT)
            titleLayoutParams.addRule(RelativeLayout.CENTER_VERTICAL)
            titleLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT)
            titleView?.layoutParams = titleLayoutParams
            layout.addView(titleView)

            layout
        }



    }

    fun onCreateCheckView():CheckBox{
        checkbox= AppCompatCheckBox(context)
            checkbox!!.setOnCheckedChangeListener { compoundButton, b ->
                value=b
            }
        customizeCheckBox.invoke(this,this.checkbox as CheckBox)
        return checkbox as CheckBox
    }
}